/**   
 * @Title: WithdrawalAct.java 
 * @Package com.esun.rabbit2.viewer.mainact.mylotteryact.withdrawal 
 * @Description: 提款功能
 * @Author: Miko baoz@500wan.com   
 * @Date: 2011-7-3 下午02:57:13   
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.view.personal;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalcommunicater.WithdrawalCommunicater;
import com.esun.cheetah.biz.personal.personalinfo.RechargeUserInfo;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Language;
import com.esun.util.EditTextInputUitl;

import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import android.widget.TextView;

/**
 * 
 * @Package com.esun.cheetah.view.personal.WithdrawalAct
 * @ClassName: WithdrawalAct
 * @Description: TODO(withdraw activity)
 * @author handy <br/>
 * @date create at 2012-7-31 下午3:02:57
 *
 */
public class WithdrawalAct extends Activity implements OnClickListener {

	private Button btnGoBack; // 返回按钮
	private Button btnFeeTip; // 提款须知

	private EditText etRealName; // 真实姓名
	private Button btnClearRealName;

	private EditText etPhoneNum; // 电话号码
	private Button btnClearPhoneNum;

	private EditText etMoney; // 提取金额
	private Button btnClearMoney;

	private TextView tvBankName; // 银行名称
	private TextView tvCardNum; // 银行卡号
	private TextView tvBalance; // 可提取金额

	private Button btnSubmit; // 提交按钮

	private String strRealName; // 真实姓名
	private String strPhoneNum; // 电话号码
	private String strBankName; // 银行名称
	private String strCardNum; // 银行卡号
	private String strMoney; // 提款金额

	// private RechargeController controller = new RechargeController();

//	private LoginTimeControl loginTimeControl;
	private WithdrawalCommunicater withdrawalCommunicater;

	private Handler handler;

	private Dialog dialogVerify; // 确认提示
	private View.OnClickListener verifyListener;// 确认提示的监听器

	// private Dialog dialogFee;// 提款须知

	private ProgressDialog progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
		// WindowManager.LayoutParams. FLAG_FULLSCREEN);
		setContentView(R.layout.withdrawal_act);

		init();
		initHandler();
	}

	private void init() {

		btnGoBack = (Button) this.findViewById(R.id.withdraw_act_btn_goBack);
		btnGoBack.setOnClickListener(this);

		btnFeeTip = (Button) this.findViewById(R.id.withdraw_act_btn_feeTip);
		btnFeeTip.setOnClickListener(this);

		btnSubmit = (Button) this.findViewById(R.id.withdrawal_act_btn_submit);
		btnSubmit.setOnClickListener(this);

		etRealName = (EditText) this
				.findViewById(R.id.withdrawal_act_et_realName);
		btnClearRealName = (Button) this
				.findViewById(R.id.withdrawal_act_et_realName_btn_claen);
		etRealName = new EditTextInputUitl(etRealName, btnClearRealName)
				.getEditText();

		etPhoneNum = (EditText) this
				.findViewById(R.id.withdrawal_act_et_phoneNum);
		btnClearPhoneNum = (Button) this
				.findViewById(R.id.withdrawal_act_et_phoneNum_btn_claen);
		etPhoneNum = new EditTextInputUitl(etPhoneNum, btnClearPhoneNum)
				.getEditText();

		etMoney = (EditText) this.findViewById(R.id.withdrawal_act_et_money);
		btnClearMoney = (Button) this
				.findViewById(R.id.withdrawal_act_et_money_btn_claen);
		etMoney = new EditTextInputUitl(etMoney, btnClearMoney).getEditText();

		tvBankName = (TextView) this
				.findViewById(R.id.withdrawal_act_tv_bankName);
		tvBankName.setText(RechargeUserInfo.getUserInfo().getBankName());

		tvCardNum = (TextView) this
				.findViewById(R.id.withdrawal_act_tv_cardNum);
		StringBuilder strShowNum = new StringBuilder();
		String[] strCardNum = RechargeUserInfo.getUserInfo().getCardNum()
				.split("|");
		for (int i = 0; i < strCardNum.length; i++) {
			if (i > 4 && i < strCardNum.length - 4) {
				strShowNum.append("*");
			} else {
				strShowNum.append(strCardNum[i]);
			}
		}
		tvCardNum.setText(strShowNum.toString());

		tvBalance = (TextView) this
				.findViewById(R.id.withdrawal_act_tv_avaliableMoney);
		tvBalance.setText(UserInfo.getUserInfo().getMoney().toString());

//		loginTimeControl = new LoginTimeControl();

		progress = ViewerUtil.showProgressDialog(this, "数据通信中，请稍后……");
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.cancel();

				switch (msg.what) {
				//
				// case WITHDRAWAL_SUCCESS:
				//
				// ViewerUtil.showTipDialog(WithdrawalAct.this,
				// UserInfo.getUserInfo().getUserName() + "，您提出的提款申请已受理！我们会尽快将"
				// + strMoney + "元转到您指定的银行卡账户。如有任何疑问请拨打客服热线咨询：0755-86330000",
				// false);
				//
				// break;
				//
				// case WITHDRAWAL_FAILED:
				// // ViewerUtil.showTipDialog(WithdrawalAct.this,
				// controller.getMSG(), false);
				// break;
				//
				// default:
				// break;

				case Config.COMMUNICATER_SUCCESS:
					EDebug.o("good--");
//					ViewerUtil.showTipDialog(WithdrawalAct.this, UserInfo.getUserInfo().getUserName() + "，您提出的提款申请已受理！我们会尽快将" + strMoney + "元转到您指定的银行卡账户。如有任何疑问请拨打客服热线咨询：0755-86330000", false);
					Toast.makeText(WithdrawalAct.this, UserInfo.getUserInfo().getUserName() + "，您提出的提款申请已受理！我们会尽快将" + strMoney + "元转到您指定的银行卡账户。如有任何疑问请拨打客服热线咨询：0755-86330000", 500).show();
					break;
				case Config.COMMUNICATER_FAIL:
//					ViewerUtil.showTipDialog(WithdrawalAct.this,
//							withdrawalCommunicater.getServerMsg(), false);
					Toast.makeText(WithdrawalAct.this, withdrawalCommunicater.getServerMsg(), 500).show();
					break;
				case Config.COMMUNICATER_EXCEPTION:
//					ViewerUtil.showTipDialog(getParent(),
//							Language.NERWORK_ABORT, false);
					Toast.makeText(WithdrawalAct.this, Language.NERWORK_ABORT, 500).show();
					EDebug.o("通讯异常" + UserInfo.getUserInfo().getFreezemoney());

					break;

				}

			}
		};
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.withdraw_act_btn_goBack:
			this.finish();
			break;

		case R.id.withdrawal_act_btn_submit:
			if (checkSubmit()) {
				// if (loginTimeControl.isNeedInputPwd()) {
				// loginTimeControl.showInputPasswordDialog(this);
				// } else {
				popVerifyNotice();
				// }
			}

			break;

		case R.id.withdraw_act_btn_feeTip:
			popFeeNotice();
			break;

		default:
			break;
		}
	}

	private boolean checkSubmit() {

		strMoney = etMoney.getText().toString();

		if (etRealName.getText().toString().length() < 1) {
//			ViewerUtil.showTipDialog(this, "请正确填写真实姓名", false);
			Toast.makeText(this, "请正确填写真实姓名", 500).show();
			return false;
		} else if (strMoney.length() < 1 || strMoney.equals("0")) {
//			ViewerUtil.showTipDialog(this, "请输入提现金额", false);
			Toast.makeText(this, "请输入提现金额", 500).show();
			return false;
		} else if (etPhoneNum.getText().toString().length() < 1) {
//			ViewerUtil.showTipDialog(this, "请输入正确的手机号码", false);
			Toast.makeText(this, "请输入正确的手机号码", 500).show();
			return false;
		} else if (Double.parseDouble(strMoney) > Double.parseDouble(UserInfo
				.getUserInfo().getMoney())) {
//			ViewerUtil.showTipDialog(this, "提款金额超过可提款金额，请重新输入", false);
			Toast.makeText(this, "提款金额超过可提款金额，请重新输入", 500).show();
			return false;
		} else {
			return true;
		}
	}

	private void popVerifyNotice() {

		if (dialogVerify == null) {
			dialogVerify = new Dialog(this, R.style.dialog);
		}

		if (verifyListener == null) {
			verifyListener = new OnClickListener() {

				@Override
				public void onClick(View v) {

					switch (v.getId()) {

					case R.id.withdrawal_alert_verify_btn_cancel:
						dialogVerify.dismiss();
						break;

					case R.id.withdrawal_alert_verify_btn_submit:

						progress.show();
						// submitInThread();
						// strRealName, strMoney, strPhoneNum
						withdrawalCommunicater = new WithdrawalCommunicater(
								handler, strRealName, strMoney, strPhoneNum);
						withdrawalCommunicater.startCommunicate();
						dialogVerify.dismiss();
						break;

					case R.id.withdrawal_alert_verify_btn_fee:
						popFeeNotice();
						break;

					default:
						break;
					}

				}
			};
		}

		View alertView = this.getLayoutInflater().inflate(
				R.layout.withdrawal_alert_verify, null);

		strMoney = etMoney.getText().toString();
		strBankName = RechargeUserInfo.getUserInfo().getBankName();
		strCardNum = RechargeUserInfo.getUserInfo().getCardNum();
		strPhoneNum = etPhoneNum.getText().toString();
		strRealName = etRealName.getText().toString();

		((Button) alertView
				.findViewById(R.id.withdrawal_alert_verify_btn_cancel))
				.setOnClickListener(verifyListener);
		((Button) alertView
				.findViewById(R.id.withdrawal_alert_verify_btn_submit))
				.setOnClickListener(verifyListener);
		((Button) alertView.findViewById(R.id.withdrawal_alert_verify_btn_fee))
				.setOnClickListener(verifyListener);

		((TextView) alertView
				.findViewById(R.id.withdrawal_alert_verify_tv_realName))
				.setText(strRealName);
		((TextView) alertView
				.findViewById(R.id.withdrawal_alert_verify_tv_bankName))
				.setText(strBankName);
		((TextView) alertView
				.findViewById(R.id.withdrawal_alert_verify_tv_money))
				.setText(strMoney);
		((TextView) alertView
				.findViewById(R.id.withdrawal_alert_verify_tv_phoneNum))
				.setText(strPhoneNum);

		StringBuilder strShowNum = new StringBuilder();
		String[] strCardNumTmp = strCardNum.split("|");
		for (int i = 0; i < strCardNumTmp.length; i++) {
			if (i > 4 && i < strCardNumTmp.length - 4) {
				strShowNum.append("*");
			} else {
				strShowNum.append(strCardNumTmp[i]);
			}
		}
		((TextView) alertView
				.findViewById(R.id.withdrawal_alert_verify_tv_cardNum))
				.setText(strShowNum.toString());

		dialogVerify.setContentView(alertView);
		dialogVerify.show();
	}

	private void popFeeNotice() {

		startActivity(new Intent().setClass(this, WithdrawalFeeAct.class));

		// if (dialogFee == null) {
		// dialogFee = new Dialog(this);
		// }
		//
		// View feeView =
		// this.getLayoutInflater().inflate(R.layout.withdrawal_tip_act, null);
		//
		// ((Button)
		// feeView.findViewById(R.id.withdrawal_tip_act_ok)).setOnClickListener(new
		// OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// dialogFee.dismiss();
		// }
		// });
		//
		// dialogFee.setContentView(feeView);
		// dialogFee.show();
	}

	// private void submitInThread() {
	// new Thread() {
	//
	// @Override
	// public void run() {
	// super.run();
	//
	// if (doSubmit()) {
	// Message msg = handler.obtainMessage(WITHDRAWAL_SUCCESS);
	// handler.sendMessage(msg);
	// } else {
	// Message msg = handler.obtainMessage(WITHDRAWAL_FAILED);
	// handler.sendMessage(msg);
	// }
	//
	// }
	// }.start();
	// }
	//
	// private boolean doSubmit() {
	// return controller.isWithdrawAmountOk(strRealName, strMoney, strPhoneNum);
	// }

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(),
		// ConnectionChangeReceiver.getIntentFilter());
	}

}
